
/*---------------------------------------------------------------------------

  FILENAME:
        cmdbufqueue.c

  PURPOSE:
	Provide a command queue buf for income socket command data.

  REVISION HISTORY:
        Date            Engineer        Revision        Remarks
	16/02/2009	Vincent. Young	1.2	cmdqueue

  NOTES:


  LICENSE:
        Copyright (c) 2009, Vincent Young (vincentgodyoung@hotmail.com)

        This source code is released for free distribution under the terms
        of the GNU General Public License.

----------------------------------------------------------------------------*/

// local project include files
#include "cmdbufqueue.h"

#define LOGREGMODE FALSE

cmdstring *firstcmd, *lastcmd;
int cmdsum;

int cmdaddlast(void *buf, int len, int socketid)
{
	cmdstring *cmdnew;
	if (len > CMDLENGTH)
	{
		printf("cmd [%d] too large\n", len);
		return -3;
	}
	if (cmdsum == MAXCMDS)
	{
		printf("cmdaddlast: cmd queue succeed max cmds!\n");
		return -4;
	}

	if(lastcmd == NULL)
	{	//��һ���ڵ�
		lastcmd = malloc(sizeof(cmdstring));
		if (firstcmd == NULL)
		{
			firstcmd = lastcmd;
#if LOGREGMODE
			printf("cmdaddlast: initialize the first cmd......\n");
#endif
		}
		else if (firstcmd != NULL)
		{
			printf("cmdaddlast: top secret!\n");
			firstcmd = lastcmd;
		}
		cmdnew = firstcmd;
	}
	else if(lastcmd != NULL)
	{	//�����ڵ�
		if (lastcmd->nextcmd != NULL)
		{
			printf("cmdaddlast: cmd queue corrupted!\n");
			return -1;
		}
		
		lastcmd->nextcmd = malloc(sizeof(cmdstring));
		cmdnew = lastcmd->nextcmd;
	}
	
	if (cmdnew == NULL)
	{
		printf("cmdaddlast: not enough memory!\n");
		return -2;
	}
	cmdnew->cmdlength = len;
	cmdnew->nextcmd = NULL;
	cmdnew->socketid = socketid;
	cmdnew->cmd = malloc(len);
	if (cmdnew->cmd == NULL)
	{
		printf("cmdaddlast: not enough memory!\n");
		return -5;
	}
	memset(cmdnew->cmd, '\0', len);
	//memcpy(cmdnew->cmd, buf, len);
	strcpy(cmdnew->cmd, buf);
	lastcmd = cmdnew;
	
#if LOGREGMODE
	printf("cmdaddlast: cmd queue got from socket [%d] [%s]\n", lastcmd->socketid, (char *)(lastcmd->cmd));
	printf("cmdaddlast: cmd queue first cmd is [%d] [%s]\n", firstcmd->socketid, (char *)(firstcmd->cmd));
#endif
	cmdsum = cmdsum + 1;
	return 0;
}

int cmddelfirst()
{
	cmdstring *delling = firstcmd;
#if LOGREGMODE
	printf("cmddelfirst: are you sure to delete cmd [%d] [%s]\n", delling->socketid, (char*)(delling->cmd));
#endif
	if (firstcmd->nextcmd != NULL)
	{
		firstcmd = firstcmd->nextcmd;
		if (delling->cmd != NULL)
		{
			free(delling->cmd);
		}
		else if (delling->cmd == NULL)
		{
			printf("cmddelfirst: first cmd null error!\n");
		}
		free(delling);
#if LOGREGMODE
		printf("cmddelfirst: the new first cmd is [%d] [%s]\n", firstcmd->socketid, (char *)(firstcmd->cmd));
#endif
		cmdsum = cmdsum - 1;
	}
	else if (firstcmd->nextcmd == NULL)
	{
#if LOGREGMODE
		printf("cmddelfirst: delating the only one cmd [%d] [%s]\n", firstcmd->socketid, (char *)(firstcmd->cmd));
#endif
		lastcmd = firstcmd;
		if (firstcmd->cmd != NULL)	free(firstcmd->cmd);
		else	printf("cmddelfirst: firstcmd null error!\n");
		firstcmd->socketid = 0;
		firstcmd->cmdlength = 0;
		free(firstcmd);
		firstcmd = NULL;
		lastcmd = NULL;
		delling = NULL;
		cmdsum = 0;
#if LOGREGMODE
		printf("cmddelfirst: command queue cleared for landing\n");
#endif
	}
	return 0;
}

